import {
  Body,
  ClassSerializerInterceptor,
  DefaultValuePipe,
  Delete,
  Get,
  Param,
  ParseIntPipe,
  Post,
  UseInterceptors,
} from '@nestjs/common';
import { ApiOperation } from '@nestjs/swagger';
import { User } from './entities/user.entity';
import { GetUser } from '../auth/get-user.decorator';
import { UserService } from './user.service';
import { GetUserFilterDto } from './dto/getUserFilterDto';
import { updateUser, UserAllDto } from './dto/userAll.dto';
import { Pagination } from '../utils/paginate';
import { WitAdmin } from '../auth/wit.admin';
import { UpdatePasswordDto } from './dto/updatePassword.dto';
@WitAdmin('user')
// 隐藏用户密码
@UseInterceptors(ClassSerializerInterceptor)
export class UserController {
  constructor(private userService: UserService) {}
  @Delete('/deleteUser/:id')
  @ApiOperation({
    summary: '删除用户',
  })
  deleteUser(@Param('id') id: string): Promise<void> {
    return this.userService.deleteUsers(id);
  }

  @Get('/getUserById/:id')
  @ApiOperation({
    summary: '获取指定ID用户信息',
  })
  getUserById(@Param('id') id: string): Promise<User> {
    return this.userService.getUserByIds(id);
  }
  @Get('/getUserInfo')
  @ApiOperation({
    summary: '获取当前登录的用户信息',
  })
  getUserInfo(@GetUser() user: User): Promise<User> {
    return this.userService.getUserInfos(user);
  }
  @Post('/updateUser')
  @ApiOperation({
    summary: '修改当前登录的用户信息',
  })
  updateUser(
    @Body() userAllDto: UserAllDto,
    @GetUser() user: User,
  ): Promise<User> {
    return this.userService.updateUsers(userAllDto, user);
  }
  @Post('/updateUserById')
  @ApiOperation({
    summary: '修改指定用户信息',
  })
  updateUserById(
    @Body() userAllDto: updateUser,
    @GetUser() user: User,
  ): Promise<User> {
    return this.userService.updateUserByIds(userAllDto, user);
  }
  @Post('/updatePassword')
  @ApiOperation({
    summary: '修改用户密码',
  })
  updateUserPassword(
    @Body() updatePassword: UpdatePasswordDto,
    @GetUser() user: User,
  ): Promise<string> {
    return this.userService.updatePassword(updatePassword, user);
  }
  @Post('/getUserList')
  @ApiOperation({
    summary: '获取所有用户信息',
  })
  // @ApiImplicitParam({
  //   name: 'pageNum',
  //   required: false,
  // })
  // @ApiImplicitParam({
  //   name: 'pageSize',
  //   required: false,
  // })
  getUserList(
    @Body() getUserFilterDto: GetUserFilterDto,
    @Param('pageNum', new DefaultValuePipe(0), ParseIntPipe) pageNum?: number,
    @Param('pageSize', new DefaultValuePipe(0), ParseIntPipe) pageSize?: number,
  ): Promise<Pagination<User>> {
    const page = {
      page: pageNum,
      limit: pageSize > 100 ? 100 : pageSize,
    };
    return this.userService.getUserLists(getUserFilterDto, page);
  }
}
